//package com.neshai.webapp;
//
//import static spark.Spark.post;
//import static spark.Spark.port;
//import com.google.gson.JsonSyntaxException;
//import com.stripe.Stripe;
//import com.stripe.exception.SignatureVerificationException;
//import com.stripe.model.StripeObject;
//import com.stripe.net.ApiResource;
//import com.stripe.model.Event;
//import com.stripe.model.EventDataObjectDeserializer;
//
//import com.stripe.model.PaymentIntent;
//import com.stripe.model.PaymentMethod;
//import com.stripe.net.Webhook;
//
//public class Test {
//    public static void main(String[] args) {
//        port(4242);
//        // This is your test secret API key.
//        Stripe.apiKey = "sk_test_51Pl2IR2LVWfSov9xP4nNQzn7oFtbKLlM7ZUhEnuTOxql3pccnWpgaJUvCvqqZ62LZL59UNyVUMSaLLnJxieXgPnf00BtlA5yyK";
//        // Replace this endpoint secret with your endpoint's unique secret
//        // If you are testing with the CLI, find the secret by running 'stripe listen'
//        // If you are using an endpoint defined with the API or dashboard, look in your webhook settings
//        // at https://dashboard.stripe.com/webhooks
//        String endpointSecret = "whsec_...";
//
//        post("/webhook", (request, response) -> {
//            String payload = request.body();
//            Event event = null;
//
//            try {
//                event = ApiResource.GSON.fromJson(payload, Event.class);
//            } catch (JsonSyntaxException e) {
//                // Invalid payload
//                System.out.println("⚠️  Webhook error while parsing basic request.");
//                response.status(400);
//                return "";
//            }
//            String sigHeader = request.headers("Stripe-Signature");
//            if(endpointSecret != null && sigHeader != null) {
//                // Only verify the event if you have an endpoint secret defined.
//                // Otherwise use the basic event deserialized with GSON.
//                try {
//                    event = Webhook.constructEvent(
//                            payload, sigHeader, endpointSecret
//                    );
//                } catch (SignatureVerificationException e) {
//                    // Invalid signature
//                    System.out.println("⚠️  Webhook error while validating signature.");
//                    response.status(400);
//                    return "";
//                }
//            }
//            // Deserialize the nested object inside the event
//            EventDataObjectDeserializer dataObjectDeserializer = event.getDataObjectDeserializer();
//            StripeObject stripeObject = null;
//            if (dataObjectDeserializer.getObject().isPresent()) {
//                stripeObject = dataObjectDeserializer.getObject().get();
//            } else {
//                // Deserialization failed, probably due to an API version mismatch.
//                // Refer to the Javadoc documentation on `EventDataObjectDeserializer` for
//                // instructions on how to handle this case, or return an error here.
//            }
//            // Handle the event
//            switch (event.getType()) {
//                case "payment_intent.succeeded":
//                    PaymentIntent paymentIntent = (PaymentIntent) stripeObject;
//                    System.out.println("Payment for " + paymentIntent.getAmount() + " succeeded.");
//                    // Then define and call a method to handle the successful payment intent.
//                    // handlePaymentIntentSucceeded(paymentIntent);
//                    break;
//                case "payment_method.attached":
//                    PaymentMethod paymentMethod = (PaymentMethod) stripeObject;
//                    // Then define and call a method to handle the successful attachment of a PaymentMethod.
//                    // handlePaymentMethodAttached(paymentMethod);
//                    break;
//                default:
//                    System.out.println("Unhandled event type: " + event.getType());
//                    break;
//            }
//            response.status(200);
//            return "";
//        });
//    }
//}
